package com.mes.web.controller.tdd;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.google.gson.Gson;
import com.mes.entity.tdd.ProcurementRukuMateriel;
import com.mes.entity.tdd.tj.Tpage;
import com.mes.service.tdd.ProcurementRukuMaterielService;
import com.mes.utils.DateJsonValueProcessor;
import com.mes.utils.ResponseUtil;
import com.mes.utils.StringUtil;
/**
 * 
 *@ClassName: ProcurementRukuMaterielController 
 *@Description: 采购到货单详情
 *@Author tdd
 *@Date 2018-6-5 下午4:17:19 
 *@version 1.0
 */
@Controller
@RequestMapping("/procurementRukuMateriel")
public class ProcurementRukuMaterielController {
	//注入到货单详情Server
	@Resource public ProcurementRukuMaterielService rukuMaterielService;
	/**
	 * 采购到货单详情分页
	 * @param pageBean 分页+条件
	 * @param limit 每页条数
	 * @param offset 条数
	 * @param response response对象
	 * @throws Exception 异常
	 */
	@RequestMapping("/list")
	public void materieDemandList(Tpage pageBean,@RequestParam(value="limit",required=false)String limit,@RequestParam(value="offset",required=false)String offset,HttpServletResponse response) throws Exception {
		
		//页数
		pageBean.setPage(Integer.parseInt(offset)/Integer.parseInt(limit));
		//每页条数
		pageBean.setPageSize(Integer.parseInt(limit));
		//未删除
		pageBean.setIsdelete("0");
		List<ProcurementRukuMateriel> list = rukuMaterielService.list(pageBean);
		int total=rukuMaterielService.total(pageBean);
		
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(java.util.Date.class,new DateJsonValueProcessor("yyyy-MM-dd"));
		JSONArray jsonArray = JSONArray.fromObject(list, jsonConfig);
		jsonObject.put("total", total);
		jsonObject.put("rows", jsonArray);
		ResponseUtil.write(response, jsonObject);
	}
	/**
	 * 采购到货单详情
	 * @param pageBean  分页+条件 
	 * @param response response对象
	 * @throws Exception 异常
	 */
	@RequestMapping("/allList")
	public void materieDemandList(Tpage pageBean,HttpServletResponse response) throws Exception {
		
		//未删除
		pageBean.setIsdelete("0");
		List<ProcurementRukuMateriel> list = rukuMaterielService.allList(pageBean);
		
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(java.util.Date.class,new DateJsonValueProcessor("yyyy-MM-dd"));
		JSONArray jsonArray = JSONArray.fromObject(list, jsonConfig);
		jsonObject.put("total", null);
		jsonObject.put("rows", jsonArray);
		ResponseUtil.write(response, jsonObject);
	}
	/**
	 * 根据到货单id查 到货单+物料
	 * @param prukuid 到货单id
	 * @return 到货单详情集合
	 */
	@RequestMapping("/find")
	public List<ProcurementRukuMateriel> findbyprukuid(String prukuid){
		/*Tpage pageBean = new Tpage();
		//未删除
		pageBean.setIsdelete("0");
		pageBean.setPrid(Long.valueOf(prukuid));
		//每页条数
		int total=rukuMaterielService.total(pageBean);
		//页数
		pageBean.setPage(0);
		pageBean.setPageSize(total);
		List<ProcurementRukuMateriel> list = rukuMaterielService.list(pageBean);*/
		ProcurementRukuMateriel rukuMateriel = new ProcurementRukuMateriel();
		rukuMateriel.setPrid(Long.valueOf(prukuid));
		List<ProcurementRukuMateriel> list = rukuMaterielService.find(rukuMateriel);
		return list;
	}
	/**
	 * 添加
	 * @param response response对象
	 * @param rukuMateriel 到货单详情
	 * @param request request对象 
	 * @throws IOException IO流异常
	 */
	@RequestMapping("/add")
	public void add(HttpServletResponse response,ProcurementRukuMateriel rukuMateriel,HttpServletRequest request) throws IOException{
    	//添加
    	Long id = rukuMaterielService.add(rukuMateriel);
    	//添加成功
    	if (null != id && id >= 1) {
    		response.getWriter().print("1");
    		//未成功
    	} else {
    		response.getWriter().print("0");
    	}
	}
	/**
	 * 到修改
	 * @param request request对象
	 * @param response response对象
	 * @return null
	 * @throws Exception 异常
	 */
	@RequestMapping("/toupdate")
	public String toupdata(HttpServletRequest request,HttpServletResponse response) throws Exception{
		//json
		Gson gson=new Gson();
		//根据id查询数据
		Tpage pageBean = new Tpage();
		//获取id
		pageBean.setStaffid(Long.valueOf((Integer.parseInt(request.getParameter("staffid")))));
		//未删除
		pageBean.setIsdelete("0");
		List<ProcurementRukuMateriel> list = rukuMaterielService.list(pageBean);
		//获得数据
		if (list.size()>0) {
			//传递数据到页面
			ResponseUtil.write(response, gson.toJson(list.get(0)));
		}
		//跳转到修改页面
		return null;
	}
	/**
	 * 修改
	 * @param rukuMateriel 到货详情
	 * @param response response对象
	 * @param request request对象
	 * @throws IOException IO流异常
	 */
	@RequestMapping("/update")
	public void updata(ProcurementRukuMateriel rukuMateriel,HttpServletResponse response,HttpServletRequest request) throws IOException{
		//根据id查数据
        //修改前数据
    	Tpage pageBean = new Tpage();
		//获取id
		pageBean.setPmid(rukuMateriel.getPmid());
		//未删除
		pageBean.setIsdelete("0");
		List<ProcurementRukuMateriel> list = rukuMaterielService.list(pageBean);
		//如果有该条数据
		if (list.size()>0) {
			ProcurementRukuMateriel rm2 = list.get(0);
			if( null!= rukuMateriel.getPmbatchnumber() && !rukuMateriel.getPmbatchnumber().equals("")){
				rm2.setPmbatchnumber(rukuMateriel.getPmbatchnumber());
			}
			if( null!= rukuMateriel.getNumber() && !rukuMateriel.getNumber().equals("")){
				rm2.setNumber(rukuMateriel.getNumber());
			}
			if( null!= rukuMateriel.getBarcode() && !rukuMateriel.getBarcode().equals("")){
				rm2.setBarcode(rukuMateriel.getBarcode());
			}
        	//修改
			int n = rukuMaterielService.updata(rm2);
			//修改成功
			if (n >= 1) {
				response.getWriter().print("1");
				//未成功
			} else {
				response.getWriter().print("0");
			}
		//如果没有该条数据(没有)
		}else {
			//该数据不存在
			response.getWriter().print("2");
		}
	}
	/**
	 * 删除
	 * @param ids 采购到货单详情id用","拼接而成的字符串
	 * @param request request对象
	 * @param response response对象
	 * @throws Exception 异常
	 */
	@RequestMapping("/delete")
	public void delete(String ids,HttpServletRequest request,HttpServletResponse response) throws Exception{
        List<Long> listId = StringUtil.splitListL(ids, ",");
        for (int i = 0; i < listId.size(); i++) {
        	//根据id查数据
        	//修改前数据
        	Tpage pageBean = new Tpage();
        	//获取id
        	pageBean.setPmid(listId.get(i));
        	pageBean.setPage(0);
        	pageBean.setPageSize(10);
        	List<ProcurementRukuMateriel> list = rukuMaterielService.list(pageBean);
        	//如果有该条数据
        	if (list.size()>0) {
        		ProcurementRukuMateriel rukuMateriel = list.get(0);
        		//是否删除指定删除
        		//删除（假删除）
        		int n = rukuMaterielService.updatadelete(rukuMateriel);
        		//删除成功
        		if (n >= 1) {
        		//未成功
        		} else {
        			response.getWriter().print("0");
        		}
        		//如果没有该条数据(没有)
        	}else {
        		//该数据不存在
        		response.getWriter().print("2");
        	}
		}
        response.getWriter().print("1");
	}	
		
}
